package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szholly.plug.form.excel.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.data.general.GlobalParameter;

@Controller
@RequestMapping("/OperationGroup/*")
public class OperationGroupController extends BaseController {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 获取运维组
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getOperatorGroupList")
	public void getOperatorGroupList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = " select *  from OC_GROUP ";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(sql, parms);
		outString(result);
	}

	/**
	 * 获取站点
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStationList")
	public void getStationList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String name = RequestUnit.getParameter("NAME");
		String guid = RequestUnit.getParameter("GUID");
		String build_status = RequestUnit.getParameter("BUILD_STATUS");
		String water_type = RequestUnit.getParameter("waterType");
		String sql = " select t1.NAME ,t1.EXP_NO,t3.group_name,t3.guid,t2.guid as ASSO_GUID from BC_SITE_INFO t1 "
				+ "left join operation_group_station_asso t2 on t1.exp_no=t2.station_id "
				+ "left join OC_GROUP t3 on t3.guid=t2.operation_group_id where 1=1 ";

		if (StringUtil.IsNotNullOrEmpty(name)) {
			sql += " and t1.NAME like '%" + name + "%'";
		}
		if(StringUtil.IsNotNullOrEmpty(water_type)){
			sql+=" and t1.station_category='"+water_type+"'";
		}
		if (StringUtil.IsNotNullOrEmpty(guid)) {
			if (guid.indexOf(",") > 0) {
				String[] strs = guid.split(",");
				sql += "and ( ";
				for (int i = 0; i < strs.length; i++) {
					if (i == 0) {
						sql += "  t2.operation_group_id ='" + strs[i] + "'";
					} else if (i == strs.length - 1) {
						sql += " or t2.operation_group_id ='" + strs[i] + "')";
					} else {
						sql += " or t2.operation_group_id ='" + strs[i] + "'";
					}
				}
			} else {
				sql += " and t2.operation_group_id ='" + guid + "'";
			}
		}
		/*if (StringUtil.IsNotNullOrEmpty(water_type)) {
			sql += " and t1.station_category = '" + water_type + "'";
		}*/
		if (StringUtil.IsNotNullOrEmpty(build_status)) {
			sql += " and t1.build_status = '" + build_status + "'";
		}

		sql += " order by t1.code";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(sql, parms);
		outString(result);
	}

	/**
	 * 获取运维组下的人员 lin 2015年10月12日
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getGroupUserList")
	public void getGroupUserList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = " select u.C_USEREALNAME,u.s_userid,ga.guid,ga.GROUP_HEAD from OC_GROUP_ASSO ga"
				+ "	LEFT JOIN safe_user u on ga.user_id = u.S_USERID" + " where 1=1";

		String groupId = RequestUnit.getString("groupid");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		if (StringUtil.IsNotNullOrEmpty(groupId)) {
			sql += " and ga.group_id = @groupid";
			parms.add(new GlobalParameter("groupid", groupId));
		}

		String result = DataUtil.getPageSelectJson(sql, parms);
		outString(result);
	}

	/**
	 * 获取所有人员列表 gislin 2015年10月12日
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getUserList")
	public void getGroupUnRelateUserList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = " select u.C_USEREALNAME,u.S_USERID from safe_user u where 1=1 ";
		String username = RequestUnit.getString("username");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();

		if (username != null && !username.equals("")) {
			sql += " and u.C_USEREALNAME like @username ";
			GlobalParameter usernameParm = new GlobalParameter("username", "%" + username + "%");
			parms.add(usernameParm);
		}

		String result = DataUtil.getPageSelectJson(sql, parms);
		outString(result);
	}

	@RequestMapping(value = "/updateUserGroupRelate")
	public void updateUserGroupRelate(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		String groupId = RequestUnit.getString("groupid");
		String useridsStr = RequestUnit.getString("userids");

		try {
			DataUtil.beginTransaction();

			String deleteRelateSql = " DELETE from OC_GROUP_ASSO WHERE GROUP_ID=@groupid ";
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			parms.add(new GlobalParameter("groupid", groupId));

			DataUtil.ExecuteUpdateSql(deleteRelateSql, parms);

			String[] userids = useridsStr.split(",");
			for (int i = 0; i < userids.length; i++) {
				String[] id_head = userids[i].split(":");
				String userid = id_head[0];
				String head = "0";
				if (id_head.length == 2) {
					head = id_head[1];
				}
				String guid = UUID.randomUUID().toString();
				String insertSql = " INSERT INTO OPERATION_GROUP_ASSO(GUID,USER_ID,GROUP_ID,GROUP_HEAD)VALUES(@guid,@userid,@groupid,@head) ";
				parms.clear();
				parms.add(new GlobalParameter("guid", guid));
				parms.add(new GlobalParameter("groupid", groupId));
				parms.add(new GlobalParameter("userid", userid));
				parms.add(new GlobalParameter("head", head));
				DataUtil.ExecuteUpdateSql(insertSql, parms);
			}
			DataUtil.commitTransaction();

		} catch (Exception e) {
			DataUtil.rollbackTransaction();
			throw e;
		}
	}

}
